/**
 * Created by liubin on 2015/09/25.
 */

'use strict';
import Request from 'superagent-bluebird-promise';

export const SELECT_USERLIST_PAGING = 'selectUserlistPaging';
export const ADD_USER = 'addUser';
export const MODIFY_USER = 'modifyUser';
export const REMOVE_USER = 'removeUser';
export const GET_USER = 'getUser';

function selectResult(json, callback) {
    return {
        type: SELECT_USERLIST_PAGING,
        status: json.status,
        data: json.data,
        draw: json.draw,
        total: json.total,
        callback
    };
}

function addResult(json, callback) {
    return {
        type: ADD_USER,
        status: json.status,
        callback
    };
}

export function selectUserListPaging(request, drawCallback, settings) {
    // 需要4个参数 查询值，排序字段，正序倒序,draw
    const draw = request.draw;
    const query = request.search.value;
    const dir = request.order[0] && request.order[0].dir || '';
    const order = request.order[0] && request.columns[request.order[0].column].data || '';
    const size = request.length;
    const start = request.start;
    const promise = Request.get('/api/user').query({
        draw,
        query,
        dir,
        order,
        size,
        start
    }).promise();
    return {
        type: SELECT_USERLIST_PAGING,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(selectResult(resp.body, drawCallback));
            }
        }
    };
}

export function addUser(data, callback) {
    const promise = Request.post('/api/user').send(data).promise();
    return {
        type: ADD_USER,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(addResult(resp.body, callback));
            }
        }
    };
}

function modifyResult(json, data, d) {
    return {
        type: MODIFY_USER,
        status: json.status,
        message: json.message,
        data,
        d
    };
}

export function modifyUser(data, d) {
    const user = data.toJS();
    const promise = Request.put(`/api/user/${user.id}`).send(user).promise();
    return {
        type: MODIFY_USER,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(modifyResult(resp.body, data, d));
            }
        }
    };
}

function removeResult(json, callback) {
    return {
        type: REMOVE_USER,
        status: json.status,
        callback
    };
}

export function removeUser(id, callback) {
    const promise = Request.del(`/api/user/${id}`).promise();
    return {
        type: REMOVE_USER,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(removeResult(resp.body, callback));
            }
        }
    };
}

function getResult(json, callback) {
    return {
        type: GET_USER,
        status: json.status,
        data: json.data,
        callback
    };
}

export function getUser(id, callback) {
    const promise = Request.get(`/api/user/${id}`).promise();
    return {
        type: GET_USER,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(getResult(resp.body, callback));
            }
        }
    };
}